import 'dart:math';
import 'dart:ui';

import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:liuxlcommon/util/string_utils.dart';
import 'package:liuxlcommon/view/my_style.dart';

///def 这种写法暂不支持
Gradient? convertCssGradient(String cssGradient) {
  // 移除空格并将逗号分隔的颜色值拆分为列表
  final colorValues = cssGradient.replaceAll(' ', '').split(',');

  final List<Color> colors = [];
  List<double> stops = [];
  Alignment begin = Alignment.centerLeft;
  Alignment end = Alignment.centerRight;
  GradientRotation? transform;

  for (var i = 0; i < colorValues.length; i++) {
    final colorValue = colorValues[i];

    if (colorValue.contains('to')) {
      // 处理渐变方向
      final direction = colorValue.split('(')[1].trim().toLowerCase();

      switch (direction) {
        case 'top':
          begin = Alignment.topCenter;
          end = Alignment.bottomCenter;
          break;
        case 'bottom':
          begin = Alignment.bottomCenter;
          end = Alignment.topCenter;
          break;
        case 'left':
          begin = Alignment.centerLeft;
          end = Alignment.centerRight;
          break;
        case 'right':
          begin = Alignment.centerRight;
          end = Alignment.centerLeft;
          break;
        case 'topleft':
          begin = Alignment.topLeft;
          end = Alignment.bottomRight;
          break;
        case 'topright':
          begin = Alignment.topRight;
          end = Alignment.bottomLeft;
          break;
        case 'totopbottom':
          begin = Alignment.topCenter;
          end = Alignment.bottomCenter;
          break;
        case 'bottomleft':
          begin = Alignment.bottomLeft;
          end = Alignment.topRight;
          break;
        case 'bottomright':
          begin = Alignment.bottomRight;
          end = Alignment.topLeft;
          break;
        case 'to top bottom':
          begin = Alignment.bottomRight;
          end = Alignment.topLeft;
          break;
        default:
          break;
      }
    } else if (colorValue.contains('deg')) {
      var direction = colorValue.split('(')[1].trim().toLowerCase();
      var trans = int.parse(direction.split('deg')[0].trim());
      transform = GradientRotation((180 - trans) * pi / 180);
    } else {
      // 处理颜色值和可能的停止点
      var colorStopValues = colorValue.split(' ');

      if (colorStopValues.length == 1) {
        String colorStr = colorStopValues[0].split('%')[0];
        // 没有指定停止点，使用默认值
        int ss = int.parse(colorStr.substring(1, 7), radix: 16);
        double tr = double.parse(colorStr.substring(7));
        if (tr == 0) {
          colors.add(Color(ss).withOpacity(0));
        } else {
          colors.add(Color(ss).withOpacity(tr / 100));
        }

        stops.add((i / (colorValues.length - 1)).toDouble());
      } else if (colorStopValues.length == 2) {
        // 指定了停止点
        colors.add(Color(
            int.parse(colorStopValues[0].substring(1, 7), radix: 16) +
                0xFF000000));
        stops.add(double.parse(colorStopValues[1].endsWith('%')
                ? colorStopValues[1].substring(0, colorStopValues[1].length - 1)
                : colorStopValues[1]) /
            100.0);
      }
    }
  }

  return LinearGradient(
    begin: begin,
    end: end,
    colors: colors,
    transform: transform,
    stops: stops,
  );
}

List<BoxShadow>? cssBoxShadow(String value) {
  List values = value.split(' ');
  if (values.length == 5) {
    return [
      BoxShadow(
          color: cssColor(values[4])!,
          offset: Offset(
            cssPx(values[0])!,
            cssPx(values[1])!,
          ),
          blurRadius: cssPx(values[2])!,
          spreadRadius: cssPx(values[3])!)
    ];
  }
}

FontWeight? cssFontWeight(String value) {
  value = value.trim();
  if (value == '100') {
    return FontWeight.w100;
  } else if (value == '200') {
    return FontWeight.w200;
  } else if (value == '300')
    return FontWeight.w300;
  else if (value == '300')
    return FontWeight.w300;
  else if (value == '400')
    return FontWeight.w400;
  else if (value == '500')
    return FontWeight.w500;
  else if (value == '600' || value == 'bold')
    return FontWeight.w600;
  else if (value == '700')
    return FontWeight.w700;
  else if (value == '800')
    return FontWeight.w800;
  else if (value == '900') return FontWeight.w900;
}

Color? cssColor(String value) {
  if (value.contains("#")) {
    String sanitizedHexColor = value.trim().replaceAll('#', '');
    sanitizedHexColor = sanitizedHexColor.length == 6
        ? 'FF$sanitizedHexColor'
        : sanitizedHexColor;
    return Color(int.parse(sanitizedHexColor, radix: 16));
  } else if (value.contains("rgba")) {
    RegExp regex = RegExp(r'rgba\((\d+),(\d+),(\d+),([0-9.]+)\)');
    Match? match = regex.firstMatch(value.trim());
    if (match != null) {
      int red = int.parse(match.group(1)!);
      int green = int.parse(match.group(2)!);
      int blue = int.parse(match.group(3)!);
      double opacity = double.parse(match.group(4)!);
      // 转换为Color对象
      return Color.fromRGBO(red, green, blue, opacity);
    }
  }
  return null;
}

double? cssFontSize(String value) {
  if (value.contains('px')) {
    List<String> l = value.trim().split('px');
    double d = double.parse(l[0]);
    return d.sp;
  } else if (StringUtils.isDouble(value.trim())) {
    return double.parse(value.trim());
  }
  return 0;
}

double? cssPx(String value) {
  if (value.contains('px')) {
    List<String> l = value.trim().split('px');
    double d = double.parse(l[0]);
    return d.w;
  } else if (StringUtils.isDouble(value.trim())) {
    return double.parse(value.trim());
  }
  return 0;
}
